package com.ctrip.study.refactor.simple;

import java.util.Enumeration;
import java.util.Vector;

class Customer {

	private String _name; // 姓名

	private Vector<Rental> _rentals = new Vector<>(); // 租借记。

	public Customer(String name) {
		_name = name;
	};

	public void addRental(Rental arg) {
		_rentals.addElement(arg);
	}

	public String getName() {
		return _name;
	}

	public String statement() {

		Enumeration<Rental> rentals = _rentals.elements();

		String result = "Rental Record for " + getName() + "\n";

		while (rentals.hasMoreElements()) {

			Rental each = (Rental) rentals.nextElement(); // 取得一笔租借记。

			result += "\t" + each.getMovie().getTitle() + "\t" +

			String.valueOf(each.getCharge()) + "\n";

		}

		// add footer lines（结尾打印）

		result += "Amount owed is " + String.valueOf(getTotalCharge()) + "\n";

		result += "You earned " + String.valueOf(getTotalFrequentRenterPoints()) +

		" frequent renter points";

		return result;

	}
	
	
	private double getTotalCharge() {
        double result = 0;
        Enumeration rentals = _rentals.elements();
        while (rentals.hasMoreElements()) {
            Rental each = (Rental) rentals.nextElement();
            result += each.getCharge();
        }
        return result;
    }

	private double getTotalFrequentRenterPoints(){
		int result = 0;
		Enumeration rentals = _rentals.elements();
		while (rentals.hasMoreElements()) {
			Rental each = (Rental) rentals.nextElement();
			result += each.getFrequentRenterPoints();
		}
		return result;
	}

}
